import requests
from lxml import etree
from matplotlib import pyplot as plt
if __name__ == '__main__':
    url = 'https://lishi.tianqi.com/jinan/index.html'
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.52'
    }
    res = requests.get(url=url,headers=headers).text
    tree = etree.HTML(res)
    div_list = tree.xpath('/html/body/div[7]/div[1]/div[13]/div/div')
    tempterature_table = []
    for div in div_list[1:]:
        li_list = div.xpath('./ul/li')
        for li in li_list:
            title = li.xpath('./a[@title]/text()')[0]
            detail_url = 'https://lishi.tianqi.com/'+li.xpath('./a/@href')[0]
            print('正在爬取：',title,detail_url,'...')
            detail = requests.get(url=detail_url,headers=headers).text
            detail_tree = etree.HTML(detail)
            detail_li_list = detail_tree.xpath('/html/body/div[7]/div[1]/div[4]/ul/li')
            for detail_li in detail_li_list:
                temperature = detail_li.xpath('./div[2]/text()')[0]
                tempterature_table.append(temperature)
                print(title,'爬取完成')
                pass
            pass
        pass
    x = range(0,len(tempterature_table))
    plt.plot(x,tempterature_table)
    plt.show()
